package cn.anson.jujuapp.activity;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.mob.mobapi.API;
import com.mob.mobapi.APICallback;
import com.mob.mobapi.MobAPI;
import com.mob.mobapi.apis.Weather;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.anson.jujuapp.R;
import cn.anson.jujuapp.Utils;


public class SecondActivity extends AppCompatActivity implements APICallback {


    private String       cityname;
    private TextView     tv_city;
    private Weather      api;
    private String       districtName;
    private StringBuffer result;
    private final String TAG = "SecondActivity";
    private TextView                           tv_time;
    private TextView                           tv_humidity;
    private TextView                           tv_dressingIndex;
    private TextView                           tv_week;
    private TextView                           tv_temperature;
    private TextView                           tv_weather;
    private TextView                           tv_wind;
    private ArrayList<HashMap<String, Object>> results;
    private HashMap<String, Object>            weather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //获取API实例
        api = (Weather) MobAPI.getAPI(Weather.NAME);

        cityname = getIntent().getStringExtra("city").substring(0, 2);//市的名称
        districtName = getIntent().getStringExtra("districtName").substring(0, 2);//区的名称
        initView();

        initData();


    }

    //创建Handler对象
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    tv_city.setText(weather.get("city") + "");//城市
                    tv_time.setText(weather.get("time") + "");//时间
                    tv_humidity.setText(weather.get("humidity") + "");//湿度
                    tv_dressingIndex.setText(weather.get("dressingIndex") + "");//穿衣指数
                    tv_week.setText(weather.get("week") + "");//日期
                    tv_temperature.setText(weather.get("temperature") + "");//温度
                    tv_weather.setText(weather.get("weather") + "");//天气
                    tv_wind.setText(weather.get("wind") + "");//风速
                    break;
                default:
                    break;
            }

        }
    };


    private void initData() {


        //获取天气信息
        api.queryByCityName(cityname, this);
    }

    /**
     * 初始化界面
     */
    private void initView() {
        //获取城市控件
        tv_city = (TextView) findViewById(R.id.city);
        //获取time控件
        tv_time = (TextView) findViewById(R.id.time);
        //获取humidity的控件
        tv_humidity = (TextView) findViewById(R.id.humidity);
        //获取dressingIndex的控件
        tv_dressingIndex = (TextView) findViewById(R.id.dressingIndex_data);
        //获取日期的控件
        tv_week = (TextView) findViewById(R.id.week_today);
        //获取温度的控件
        tv_temperature = (TextView) findViewById(R.id.temperature);
        //获取天气的控件
        tv_weather = (TextView) findViewById(R.id.weather);
        //获取风向的控件
        tv_wind = (TextView) findViewById(R.id.wind);

    }


    /*---------MobApi的接口回调-----*/
    @Override
    public void onSuccess(API api, int i, Map<String, Object> map) {
        switch (i) {
            case Weather.ACTION_CITYS:

                break;
            case Weather.ACTION_QUERY:
                //显示查询的结果

                showResult(map);
                break;
            default:
                break;
        }

    }

    /**
     * 显示结果
     *
     * @param map
     */
    private void showResult(Map<String, Object> map) {

        for (String key : map.keySet()) {
            Log.d(TAG, "key= " + key + " and value= " + map.get(key));
        }


        //封装结果

        results = (ArrayList<HashMap<String, Object>>) map.get("result");
        weather = results.get(0);
        //拼接结果
        result = new StringBuffer();
        if (weather != null) {
            result.append("空气质量: " + weather.get("airCondition") + "\n");
            result.append("城    市    : " + weather.get("city") + "\n");
            result.append("感冒指数     : " + weather.get("coldIndex") + "\n");
            result.append("日    期     : " + weather.get("date") + "\n");
            result.append("地    区     : " + weather.get("distrct") + "\n");
            result.append("洗衣指数     : " + weather.get("washIndex") + "\n");
            result.append("穿衣指数     : " + weather.get("dressingIndex") + "\n");
            result.append("湿    度     : " + weather.get("humidity") + "\n");
            result.append("温    度     : " + weather.get("temperature") + "\n");
            result.append("天    气     : " + weather.get("ic_launcher") + "\n");
            result.append("星    期     : " + weather.get("week") + "\n");
            result.append("风    向     : " + weather.get("wind") + "\n");
        }

        //向handler 发送消息
        Message message = handler.obtainMessage();
        message.obj = result.toString();
        message.what = 1;
        handler.sendMessage(message);
    }

    @Override
    public void onError(API api, int i, Throwable throwable) {
        Log.d(TAG, "onError----");
        //打印错误信息
        throwable.printStackTrace();

        Toast.makeText(Utils.getContext(), "获取数据失败", Toast.LENGTH_SHORT).show();
    }
    /*---------end-----*/
}
